<?php

class Mads extends CI_Model {
	
	public $table = 'ads';
	public $pri_key="id";
	
	function __construct()

	{

		parent::__construct();

	}
	//****************** Product    *************************//
	
	
	public function product_from_cate($num,$off,$id) 
	{
		$this->db->limit($off,$num);
		$this->db->where("sub_id =",$id);
		$this->db->order_by($this->pri_key, "desc"); 
		return $this->db->get($this->table)->result_array();
	}
	public function get_left($type)
	{
	$this->db->where("actived = 1");
		$this->db->where("position = 'left'" );
		$this->db->where("type = ".$type); // form : 1 ; logo : 2
		
		$this->db->order_by("priority ASC");
		return $this->db->get($this->table)->result_array();
	}
	public function get_footer()
	{
		$this->db->where("actived = 1");
		$this->db->where("position = 'bottom'" );
		//$this->db->where("type = ".$type); // form : 1 ; logo : 2
		$this->db->order_by("priority ASC");
		return $this->db->get($this->table)->result_array();
	}
	public function get_right($type)
	{
	$this->db->where("actived = 1");
		$this->db->where("position = 'right'" );
		$this->db->where("type = ".$type); // form : 1 ; logo : 2
		$this->db->order_by("priority ASC");
		return $this->db->get($this->table)->result_array();
	}

	public function get_cate()
	{
		return $this->db->get($this->table_cate)->result_array();
	}
	public function get_unit()
	{
	return $this->db->get("unit")->result_array();
	}
	
	
	function list_data($num,$off)
	{
		$this->db->limit($off,$num);
		
		if($this->db->count_all($this->table) >0)
			{
				//$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->order_by($this->pri_key, "desc"); 

				
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	function insert($data)
	{
		if($this->db->insert($this->table,$data))
			return true;
		return false;
	
	}
	// update product category
	function update_product($data,$id)
	{
		if(is_numeric($id) && $id !='')
		{
			$this->db->where($this->pri_key,$id);
			if($this->db->update($this->table,$data))
				return true;
			return false;
			
		}
		return false;
	}
	
		function delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				$row = $this->info_from_id($id,$this->table);

		
			//	print_r($row);
				//xoa file

				if ($row[0]['image'] != 'null') unlink("./upload/product/".$row[0]['image']);
			//	echo "<br /> ./upload/product/".$row[0]['prod_img'];
		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 
				//$this->db->query($sql);

			}			

		}

	}
	

	
	
	// get info from id 
	public function info_from_id($id)
	{

		{
			$pri_key=$this->pri_key;
		}
	
		if(is_numeric($id) && $id !="")
		{
			
			$this->db->where($this->pri_key.' =',$id);
//			$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
			return $this->db->get($this->table)->row_array();
		}
		else
			return false;
	}
	public function count_event($type)
	{
		$this->db->select('*')->from($this->table)->where($type,1);		
		$query = $this->db->get();
		return $query->num_rows();
	}
	
	public function get_event($type,$num='',$off='')
	{
		
		$this->db->select('*')->from($this->table)->where($type,1)->limit($off, $num);
		$this->db->order_by($this->pri_key." desc");
		$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	
	}
	function others_product($cateid,$id)
	{
		$this->db->where("sub_id",$cateid);
		$this->db->where("id != ".$id);
		$this->db->limit(10);
		$this->db->order_by($this->pri_key." desc");
		$query = $this->db->get($this->table);
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	}

	
	// dem so record 
	function count_all()
	{
		return $this->db->count_all_results($this->table);
	}
	
	function search ($keyword , $type,$num='',$off='')
	{
		//$this->db->select('*')->from($this->table)->where($type,)->limit($off, $num);
		
			$this->db->like("name",$keyword);
		$this->db->order_by($this->pri_key." desc");
		$query= $this->db->get($this->table);
		//$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
		
	}



 

}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>